home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / hipass.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  178 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    hipass -
  19.  *        Hi pass filter rows using a 3x3 kernel.
  20.  *
  21.  *                Paul Haeberli - 1989
  22.  *
  23.  */
  24. #include "stdio.h"
  25. #include "hipass.h"
  26.  
  27. static xblur();
  28. static extrap();
  29.  
  30. highpass *newhp(getfunc,xsize,ysize,mag)
  31. int (*getfunc)();
  32. int xsize, ysize;
  33. float mag;
  34. {
  35.     highpass *hp; 
  36.     int i;
  37.  
  38.     hp = (highpass *)mymalloc(sizeof(highpass));
  39.     hp->getfunc = getfunc;
  40.     hp->xsize = xsize;
  41.     hp->ysize = ysize;
  42.     hp->y = 0;
  43.     hp->extrapval = 1024*mag;
  44.     for(i=0; i<3; i++)
  45.         hp->blurrows[i] = (short *)mymalloc(xsize*sizeof(short));
  46.     for(i=0; i<2; i++)
  47.         hp->pastrows[i] = (short *)mymalloc(xsize*sizeof(short));
  48.     hp->acc = (short *)mymalloc(xsize*sizeof(short));
  49.     if(xsize<3 || ysize<3 || hp->extrapval == 0) 
  50.     hp->active = 0;
  51.     else {
  52.     hp->active = 1;
  53.     }
  54.     return hp;
  55. }
  56.  
  57. freehp(hp)
  58. highpass *hp; 
  59. {
  60.     int i;
  61.  
  62.     for(i=0; i<3; i++)
  63.         free(hp->blurrows[i]);
  64.     for(i=0; i<2; i++)
  65.         free(hp->pastrows[i]);
  66.     free(hp->acc);
  67.     free(hp);
  68. }
  69.  
  70. hpgetrow(hp,buf,y)
  71. highpass *hp;
  72. short *buf;
  73. int y;
  74. {
  75.     short *temp;
  76.     int iy;
  77.  
  78.     if(y == 0)
  79.     hp->y = 0;
  80.     if(hp->y != y) {
  81.     fprintf(stderr,"hpgetrow: y error\n");
  82.     exit(1);
  83.     }
  84.     if(hp->active) {
  85.     if (y == 0) {
  86.         bzero(hp->acc,hp->xsize*sizeof(short));
  87.         for(iy=0; iy<2; iy++) {
  88.         temp = hp->pastrows[0];
  89.         hp->pastrows[0] = hp->pastrows[1];
  90.         hp->pastrows[1] = temp;
  91.         hp->getfunc(hp->pastrows[1],iy);
  92.         xblur(hp->blurrows[iy],hp->pastrows[1],hp->xsize);
  93.         addsrow(hp->acc,hp->blurrows[iy],hp->xsize);
  94.         }
  95.         copyrow(hp->pastrows[0],buf,hp->xsize);
  96.         extrap(buf,hp->acc,6,hp->xsize,hp->extrapval);
  97.     } else if (y == hp->ysize-1) {
  98.         copyrow(hp->pastrows[1],buf,hp->xsize);
  99.         extrap(buf,hp->acc,6,hp->xsize,hp->extrapval);
  100.     } else {
  101.         temp = hp->pastrows[0];
  102.         hp->pastrows[0] = hp->pastrows[1];
  103.         hp->pastrows[1] = temp;
  104.         hp->getfunc(hp->pastrows[1],y+1);
  105.         xblur(hp->blurrows[2],hp->pastrows[1],hp->xsize);
  106.         addsrow(hp->acc,hp->blurrows[2],hp->xsize);
  107.         copyrow(hp->pastrows[0],buf,hp->xsize);
  108.         extrap(buf,hp->acc,9,hp->xsize,hp->extrapval);
  109.         subsrow(hp->acc,hp->blurrows[0],hp->xsize);
  110.         temp = hp->blurrows[0];
  111.         hp->blurrows[0] = hp->blurrows[1];
  112.         hp->blurrows[1] = hp->blurrows[2];
  113.         hp->blurrows[2] = temp;
  114.     }
  115.     } else {
  116.     hp->getfunc(buf,y);
  117.     }
  118.     hp->y++;
  119. }
  120.  
  121. /*
  122.  *    extrap -
  123.  *        Extrapolate from the blurred image beyond the original
  124.  *
  125.  */
  126. static extrap(pix,acc,div,n,extrapval)
  127. unsigned short *pix;
  128. short *acc;
  129. int div, n;
  130. int extrapval;
  131. {
  132.     int delta, val;
  133.     int mul, ext;
  134.  
  135.     mul = div;
  136.     div = div*256;
  137.     ext = extrapval;
  138.     while(n--) {
  139.     delta = (*pix * mul)-*acc++;
  140.     val = *pix+(ext*delta)/div;
  141.     if(val<0)
  142.         val = 0;
  143.     if(val>255)
  144.         val = 255;
  145.     *pix++ = val;
  146.     }
  147. }
  148.  
  149. /*
  150.  *    xblur -
  151.  *        Blur a row in the x direction
  152.  *
  153.  */
  154. static xblur(sptr,cptr,n)
  155. unsigned short *sptr, *cptr;
  156. int n;
  157. {
  158.     short acc;
  159.  
  160.     if (n<3) {
  161.     fprintf(stderr,"xblur: n may not be less than 3\n");
  162.     exit(1);
  163.     }
  164.     acc = cptr[0] + cptr[1];
  165.     *sptr++ = (3*acc+1)>>1;
  166.     acc += cptr[2];
  167.     n-=3;
  168.     while(n--) {
  169.     *sptr++ = acc;
  170.     acc -= cptr[0];
  171.     acc += cptr[3];
  172.     cptr++;
  173.     }
  174.     *sptr++ = acc;
  175.     acc -= cptr[0];
  176.     *sptr++ = (3*acc+1)>>1;
  177. }
  178.